package cn.sciento.farm.devicemanager.service.impl;

import cn.sciento.farm.devicemanager.dao.entity.DevAlarmLog;
import cn.sciento.farm.devicemanager.dao.mapper.DevAlarmLogMapper;
import cn.sciento.farm.devicemanager.dao.model.filter.AlarmLogFilter;
import cn.sciento.farm.devicemanager.service.AlarmLogService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class AlarmLogServiceImpl implements AlarmLogService {

    @Autowired
    private DevAlarmLogMapper devAlarmLogMapper;

    @Override
    public int create(DevAlarmLog log) {
        return devAlarmLogMapper.insert(log);
    }

    @Override
    public int updateById(DevAlarmLog log) {
        return devAlarmLogMapper.updateById(log);
    }

    @Override
    public IPage<DevAlarmLog> getPage(AlarmLogFilter filter) {
        QueryWrapper<DevAlarmLog> queryWrapper = new QueryWrapper<>();
        if(filter.getAreaId() != null){
            queryWrapper.eq("area_id",filter.getAreaId());
        }
        if(filter.getAlarmId() != null){
            queryWrapper.eq("alarm_id",filter.getAlarmId());
        }
        if(filter.getCollectionId() != null){
            queryWrapper.eq("collection_id",filter.getCollectionId());
        }
        return devAlarmLogMapper.selectPage(new Page<>(filter.getPage(), filter.getSize()), queryWrapper);
    }

    @Override
    public Integer getByAlarmId(Long alarmId, long todayZeroTime, long todayLasTime) {
        QueryWrapper<DevAlarmLog> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("alarm_id",alarmId);
        queryWrapper.gt("create_time", todayZeroTime);
        queryWrapper.lt("create_time", todayLasTime);
        return devAlarmLogMapper.selectCount(queryWrapper);
    }

    @Override
    public int batchDeleteLogByCreateTime(long startTime) {
        return devAlarmLogMapper.batchDeleteLogByCreateTime(startTime);
    }

}
